<?php
function validateUsername($username)
{
	$isUsernameValid = preg_match('/^[a-z0-9]{6,30}$/', $username);

	if($isUsernameValid)
	{
		return true;
	}				
	return false;   
}

function validatePassword($password)
{
	$isPasswordValid = preg_match('/[a-z0-9]{6,30}/', $password);
					   
	if($isPasswordValid)
	{
		return true;
	}				
	return false; 				   
}

function validateMoney($money)
{
	if($money > 0)
	{
		return true;
	}
	return false;
}
function isUsernameFree($username)
{
	$db_connect = mysqli_connect("localhost", "root", "", "tractors");
	$query = "SELECT `username` FROM `user` WHERE username = '$username'";
				
	$result = mysqli_query($db_connect, $query) or die(mysqli_error($db_connect));
	mysqli_close($db_connect);
	if(mysqli_num_rows($result) > 0)
	{
		return false;
	}
	return true;
}

function insertUser($username, $pass, $money)
{
	$db_connect = mysqli_connect("localhost", "root", "", "tractors");
	$cryptPass = md5($pass);
	$query = "insert into user(`username`, `password`, `money`) values ('$username', '$cryptPass', '$money')";
				
	$result = mysqli_query($db_connect, $query) or die(mysqli_error($db_connect));
	mysqli_close($db_connect);
}

function validateUser($username, $pass)
{
	$db_connect = mysqli_connect("localhost", "root", "", "tractors");
	$cryptPass = md5($pass);
	$query = "select username, password from user where username = '$username' and password= '$cryptPass'";
				
	$result = mysqli_query($db_connect, $query) or die(mysqli_error($db_connect));
	mysqli_close($db_connect);
	
	if(mysqli_num_rows($result) > 0)
	{
		return true;
	}
	return false;
}

//check if user is logged
function isUserLoged()
{
	if(isset($_SESSION['username']) && !empty($_SESSION['username'])) {
		return true;
	} else return false;
}
?>
